package com.demo.service.serviceImpl;

import com.demo.common.Pager;
import com.demo.common.StringUtil;
import com.demo.dao.TabUsersDao;
import com.demo.model.TabUsers;
import com.demo.service.TabUsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class TabUsersServiceImpl implements TabUsersService{

    @Resource
    private TabUsersDao dao;


    @Override
    public int total_users() {
        return dao.total_users();
    }

    @Override
    public void findAll(TabUsers t, HttpServletRequest request) {
        String url="tab_users_view?loginname="+t.getLoginname();
        int size=20;
        if(StringUtil.isNotEmpty(t.getLoginname()))
            t.setLoginname("%"+t.getLoginname()+"%");
        int rows=dao.countByExampleSearch(t);
        int cpage= request.getParameter("pager") == null ? 1 : Integer
                .parseInt(request.getParameter("pager"));
        if(cpage==0) {
            cpage=1;
        }
        String pager= Pager.getPagerStr(url, size, rows, cpage, 1);
        request.setAttribute("pager", pager);
        TabUsers query=new TabUsers(t.getLoginname(),size*(cpage-1),size,"id","desc");
        List<TabUsers> list=dao.searchByPage(query);
        request.setAttribute("rows",rows);
        request.setAttribute("list", list);
    }

    @Override
    public boolean save(TabUsers t) {
        return dao.save(t);
    }

    @Override
    public List<TabUsers> findByloginname(String loginname) {
        return dao.findByloginname(loginname);
    }

    @Override
    public void update_flag(TabUsers t) {
        dao.update_flag(t);
    }

    @Override
    public TabUsers findByid(String id) {
        return dao.findByid(id);
    }

    @Override
    public boolean update(TabUsers t) {
        return dao.update(t);
    }

    @Override
    public void del(String id) {
        dao.del(id);
    }


}
